package controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import pojo.Dept;
import pojo.Page;
import pojo.UserInfo;
import service.UserInfoService;

import com.alibaba.fastjson.JSON;

@Controller
public class InfoController {
	
	@Autowired
	private UserInfoService service;
	
	@RequestMapping(value="/login",produces="application/json;charset=utf-8",method=RequestMethod.POST)
	public String login(String account,String password,HttpServletRequest request){
		UserInfo infoById = service.getInfoById(account, password, 0);
		if(infoById != null)
			return "forward:/list";
		else{
			return "index";
		}
	}
	
	@RequestMapping(value="/img",produces="application/json;charset=utf-8",method=RequestMethod.GET)
	@ResponseBody
	public String img(String account){
		UserInfo infoById = service.fan(account);
		if(infoById != null)
				return JSON.toJSONString(infoById.getPath());
		return null;
	}
	
	@RequestMapping(value="/list")
	public String list(@RequestParam(value="pageNo",required = false,defaultValue="1")int pageNo,
			@RequestParam(value="name",required = false)String name,
			@RequestParam(value="deptId",defaultValue="0",required = false)int deptId,
			HttpSession session,Model model){
		List<Dept> list = service.getAll();
		Page page = service.getPage(name, deptId, pageNo, 3);
		if(name != null && !name.equals("")){
			model.addAttribute("name",name);
		}
		if(deptId != 0){
			model.addAttribute("deptId",deptId);
		}
		model.addAttribute(page);
		session.setAttribute("list", list);
		return "list";
	}
	
	@RequestMapping("/cheName")
	@ResponseBody
	public Object cheName(String name){
		UserInfo infoById = service.getInfoById(name, null, 0);
		if(infoById == null){
			return "true";
		}
		return "false";
	}
	
	@RequestMapping(value = "/add",method = RequestMethod.POST)
	public String dd(UserInfo info,@RequestParam("a_path")MultipartFile num,
			HttpServletRequest request){
		if(!num.isEmpty()){
			String name = num.getOriginalFilename();
			File file = new File(request.getSession().getServletContext().getRealPath("/upload"),name);
			if(!file.exists()){
				file.mkdirs();
			}
			try {
				num.transferTo(file);
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			info.setPath("upload/"+name);
		}
		info.setCreateDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		boolean insert = service.insert(info);
		if(insert)
			return "forward:login";
		return "add";
	}
	@RequestMapping(value = "/add",method = RequestMethod.GET)
	public String ad(Model model){
		List<Dept> all = service.getAll();
		model.addAttribute("list",all);
		return "add";
	}
	
	@RequestMapping(value="/getupdate",method=RequestMethod.GET)
	public String upda(int id,Model model){
		List<Dept> all = service.getAll();
		model.addAttribute("list",all);
		UserInfo infoById = service.getInfoById(null, null, id);
		model.addAttribute("info",infoById);
		return "modify";
	}
	
	@RequestMapping(value="modify",method = RequestMethod.POST)
	public String update(UserInfo info,HttpServletRequest request){
		boolean update = service.update(info);
		if(update)
			return "redirect:/list";
		else
			return "modify";
	}
	
	
	@RequestMapping(value="/del")
	@ResponseBody
	public String del(int id){
		boolean del = service.del(id);
		if(del){
			return "true";
		}
		else{
			return "false";
		}
	}

}
